package it.upnp.main;
import it.upnp.controlpoint.ControlPointUtilities;
import it.upnp.controlpoint.MyControlPoint;
import it.upnp.device.DeviceUtilities;
import it.upnp.device.LightDevice;

import org.apache.log4j.Logger;
import org.cybergarage.upnp.Action;
import org.cybergarage.upnp.ActionList;
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.DeviceList;
import org.cybergarage.upnp.Service;
import org.cybergarage.upnp.ServiceList;
import org.cybergarage.upnp.ServiceStateTable;
import org.cybergarage.upnp.StateVariable;
import org.cybergarage.upnp.device.InvalidDescriptionException;


public class UPnPProjectMain {
	
	private static final Logger logger = Logger.getLogger(UPnPProjectMain.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		/*****************************************/
		/**************** Devices ****************/
		/*****************************************/
		
		try {
			
			LightDevice lightDevice = new LightDevice();
			logger.debug("LightDevice created");
			
			lightDevice.start();
			logger.debug("LightDevice started");
			
			logger.debug("\nDevices");
			DeviceUtilities.printDevices(lightDevice, logger);
			logger.debug("\nServices");
			DeviceUtilities.printServices(lightDevice, logger);
			
		} catch (InvalidDescriptionException e) {
			logger.error("Error during LightDevice creation: ", e);
		}
		
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
		
		
		/******************************************/
		/************** ControlPoint **************/
		/******************************************/
		
		MyControlPoint controlPoint = new MyControlPoint();
		controlPoint.start();
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		ControlPointUtilities.printDevices(controlPoint, logger);
		
		/* Retrieve LightDevice */
		Device lightDevice = controlPoint.getDevice("Light Device");
		
		/* Query power variable */
		StateVariable powerVar = lightDevice.getStateVariable("Power");
		if(powerVar.postQuerylAction() == true) {
			logger.debug("powerVar = " + powerVar.getValue());
		}
		else {
			logger.debug("Unable to read powerVar value");
		}
		
		/* Perform SetPower action */
		Action setPowerAct = lightDevice.getAction("SetPower");
		setPowerAct.setArgumentValue("Power", "true");
		if(setPowerAct.postControlAction() == true) {
			logger.debug("SetPower action performed");
		}
		else {
			logger.debug("Error performing SetPower action");
		}
		
		/* Query power variable */
		if(powerVar.postQuerylAction() == true) {
			logger.debug("powerVar = " + powerVar.getValue());
		}
		else {
			logger.debug("Unable to read powerVar value");
		}
		
		/* Perform PowerOff action */
		Action setPowerOff = lightDevice.getAction("PowerOff");
		if(setPowerOff.postControlAction() == true) {
			logger.debug("PowerOff action performed");
		}
		else {
			logger.debug("Error performing PowerOff action");
		}
		
		/* Query power variable */
		if(powerVar.postQuerylAction() == true) {
			logger.debug("powerVar = " + powerVar.getValue());
		}
		else {
			logger.debug("Unable to read powerVar value");
		}
	}
}
